//
//  ChangeMessageInfo+Filter.swift
//  GerritHermes
//
//  Created by J.Zhou on 2020/1/20.
//  Copyright © 2020 J.Zhou. All rights reserved.
//

import Foundation

extension ChangeMessageInfo {
    enum MessageType {
        // ignore
        case overtime               // 两倍于轮询时间间隔的算作超时
        case commentNumMarking      // 标记reply涉及的comments数量
        // notify reviewers
        case addReviewer            // 增加reviewer
        case deleteReviewer         // 删除reviewer
        case addCC                  // 增加CC
        case deleteCC               // 删除CC
        case addAssignee            // 增加assignee
        case deleteAssignee         // 删除assignee
        case newPatchSet(Int, Bool) // 有新的patch更新(patchNumber, isRebase)
        // notify owner
        case deleteVote             // 删除了vote
        case codeReview             // 增加了vote
        case reply                  // 评论的message
    }

    var messageType: MessageType {
        let overtimeDate = date.addingTimeInterval(OvertimeInterval)
        // 修正时间和服务器保持一致
        let currentDate = Date().addingTimeInterval(60 * 60 * 8)
        if overtimeDate < currentDate {
            return .overtime
        }

        if message.hasPrefix("added to REVIEWER:") {
            return .addReviewer
        }
        if message.hasPrefix("added to REMOVED:") || message.hasPrefix("removed from REVIEWER:") || tag == "autogenerated:gerrit:deleteReviewer" {
            return .deleteReviewer
        }
        if tag == "autogenerated:gerrit:setAssignee" {
            return .addAssignee
        }
        if tag == "autogenerated:gerrit:deleteAssignee" {
            return .deleteAssignee
        }
        if message.hasPrefix("added to CC:") {
            return .addCC
        }
        if message.hasPrefix("removed from CC:") {
            return .deleteCC
        }
        if message.grep(pattern: "Patch Set ([0-9]+): Code-Review").isMatched {
            return .codeReview
        }
        if message.hasPrefix("Patch Set") && (message.hasSuffix(" comment)") || message.hasSuffix(" comments)")){
            return .commentNumMarking
        }
        if tag == "autogenerated:gerrit:newPatchSet" {
            if let patchNumber = revisionNumber {
                if message.grep(pattern: "was rebased").isMatched {
                    return .newPatchSet(patchNumber, true)
                }
                return .newPatchSet(patchNumber, false)
            }
            return .newPatchSet(0, false)
        }
        if tag == "autogenerated:gerrit:deleteVote" {
            return .deleteVote
        }
        return .reply
    }
}

extension ChangeMessageInfo.MessageType {
    var isPatchSet: Bool {
        switch self {
        case .newPatchSet(_):
            return true
        default:
            return false
        }
    }

    var patchSetNumber: Int? {
        switch self {
        case .newPatchSet(let number, _):
            return number
        default:
            return nil
        }
    }

    var isRebase: Bool {
        switch self {
        case .newPatchSet(_, let isRebase):
            return isRebase
        default:
            return false
        }
    }

    var ignorable: Bool {
        switch self {
        case .overtime, .commentNumMarking:
            return true
        default:
            return false
        }
    }

    var notifyReviewers: Bool {
        switch self {
        case .addReviewer, .deleteReviewer, .addCC, .deleteCC, .addAssignee, .deleteAssignee, .newPatchSet:
            return true
        default:
            return false
        }
    }

    var notifyOwner: Bool {
        switch self {
        case .deleteVote, .codeReview, .reply:
            return true
        default:
            return false
        }
    }
}
